﻿using StoryTeller.Domain;
using StoryTeller.Domain.Exceptions;
using StoryTeller.Domain.Repository;
using StoryTeller.Infrastructure.DataAccess.DAL;
using StoryTeller.Infrastructure.DataAccess.DAL.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StoryTeller.Repository
{
    public class GroupRepository : BaseRepository<Group>, IGroupRepository
    {
        public IGroupDAL dal { get { return DALFactory.GetGroupDAL(); } }

        public void Insert(Group entidade)
        {
            try
            {
                if (entidade.Validate())
                    throw new ValidationMessagesException(entidade.ValidationMessages);

                dal.Insert(entidade);
            }
            catch (ValidationMessagesException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new FriendlyErrorMessageException(Resources.Error.ErrorMessages.GroupInsertError, ex);
            }
        }

        public void Update(Group entidade)
        {
            try
            {
                if (entidade.Validate())
                    throw new ValidationMessagesException(entidade.ValidationMessages);

                dal.Update(entidade);
            }
            catch (ValidationMessagesException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new FriendlyErrorMessageException(Resources.Error.ErrorMessages.GroupUpdateError, ex);
            }
        }

        public void Delete(int id)
        {
            try
            {
                dal.Delete(id);
            }
            catch (Exception ex)
            {
                throw new FriendlyErrorMessageException(Resources.Error.ErrorMessages.GroupDeleteError, ex);
            }
        }

        public ICollection<Group> Select()
        {
            try
            {
                return dal.Select();
            }
            catch (Exception ex)
            {
                throw new FriendlyErrorMessageException(Resources.Error.ErrorMessages.GroupSelectAllError, ex);
            }
        }

        public Group Select(int id)
        {
            try
            {
                return dal.Select(id);
            }
            catch (Exception ex)
            {
                throw new FriendlyErrorMessageException(Resources.Error.ErrorMessages.GroupSelectByIdError, ex);
            }
        }

        public ICollection<ContributorRequest> SelectContributorRequests(Group group)
        {
            try
            {
                return dal.SelectContributorRequests(group);
            }
            catch (Exception ex)
            {
                throw new FriendlyErrorMessageException(Resources.Error.ErrorMessages.GroupSelectAllError, ex);
            }
        }

        public void AddContributorRequest(ContributorRequest contributorRequest)
        {
            try
            {
                dal.AddContributorRequest(contributorRequest);
            }
            catch (Exception ex)
            {
                throw new FriendlyErrorMessageException(Resources.Error.ErrorMessages.AddContributorRequest, ex);
            }
        }

        public void AcceptContributorRequest(ContributorRequest contributorRequest)
        {
            try
            {
                dal.AcceptContributorRequest(contributorRequest);
            }
            catch (Exception ex)
            {
                throw new FriendlyErrorMessageException(Resources.Error.ErrorMessages.AcceptContributorRequestError, ex);
            }
        }
    }
}
